package testng.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.FlowEngineApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * @author wuliepeng
 * @date 2019-07-24
 */
@SpringBootTest(classes = FlowEngineApplication.class)
public class FlowWorkingDayControllerTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @BeforeClass
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    //@Test
    public void testAddWorkingDayAndOffDay() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/calendar/addWorkingDayAndOffDay").characterEncoding("UTF-8")
                .content("{\n" +
                        " \"workingDays\":[\n" +
                        "     \"2\",\n" +
                        "     \"3\",\n" +
                        "     \"4\",\n" +
                        "     \"5\",\n" +
                        "     \"6\"\n" +
                        "   ],\n" +
                        "\"offDays\":[\n" +
                        "    {\n" +
                        "       \"start\":\"2019-07-20 00:00:00\",\n" +
                        "       \"type\":\"WORKING_DAY\",\n" +
                        "       \"remark\":\"补班1\"\n" +
                        "    }\n" +
                        "  ]\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testListWorkingDay() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/calendar/listWorkingDay").characterEncoding("UTF-8")
                .contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testListOffDayAndException() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/calendar/listOffDayAndException").characterEncoding("UTF-8")
                .content("{\n" +
                        "    \"year\":\"2019\",\n" +
                        "    \"month\":\"07\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }
}